<?php

// start the session
session_start();

// start buffering the output
ob_start();

// require the database and login libraries
require_once("database.php");
require_once("liblogin.php");

$registrationErrors = "";

// check the $_POST variables
if(!empty($_POST['submitted']))
{
	if($_POST['submitted'] == "true")
	{
		// sanitize the $_POST variables
		$username = sanitizeInput($_POST['regUsername']);
		$password = sanitizeInput($_POST['regPassword']);
		$password2 = sanitizeInput($_POST['regPassword2']);
		$email = sanitizeInput($_POST['regEmail']);
		$firstName = sanitizeInput($_POST['regFirstName']);
		$lastName = sanitizeInput($_POST['regLastName']);
		
		// check whether the username already exists in the database
		if(usernameExists($username))
		{
			// the username already exists
			$registrationErrors .= "That username already exists in the database<br />";
		}
		
		// check whether the email already exists in the database
		if(emailExists($email))
		{
			// the email already exists
			$registrationErrors .= "That email already exists in the database<br />";
		}
		
		// check whether the passwords match
		if($password != $password2)
		{
			$registrationErrors .= "The password and re-typed password do not match<br />";
		}
		
		// check whether any input fields are empty
		if(empty($username)) {$registrationErrors .= "You must enter a username<br />";}
		if(empty($password)) {$registrationErrors .= "You must enter a password<br />";}
		if(empty($email)) {$registrationErrors .= "You must enter your email<br />";}
		if(empty($firstName)) {$registrationErrors .= "You must enter your first name<br />";}
		if(empty($lastName)) {$registrationErrors .= "You must enter your last name<br />";}
		
		// register the user if no errors have occurred
		if(empty($registrationErrors))
		{
			processRegistration($username, $password, $email, $firstName, $lastName);
		}
	}
}
?>

<html>
<head>
<title>UC Irvine | Professor Assignment System - Register</title>
</head>

<body>

<?php echo createFixedMenu(); ?>

<div align="center" id="registrationContainer">

<?php

if(empty($registrationErrors) && !empty($_POST['submitted']))
{
	// it was a valid registration with no errors
	echo "<h3>Confirmation</h3><br />";
	echo "Your registration was successful.  You may now <a href='login.php'>login</a>.";
}
else
{
	// non-successful registration or the form hasn't been submitted yet
?>

<h3>Register</h3><br />

<form name="loginForm" method="POST" action="<?php echo $_SERVER['SCRIPT_FILENAME']; ?>">

	<span>(*) denotes a required field</span><br /><br />
	
	<?php
	
	// the user has made an error if this clause gets executed
	if(!empty($_POST['submitted']))
	{
		echo "<span name=\"errorLabel\" style=\"color:red;\">";
		echo "$registrationErrors <br />";
		echo "</span>";
	}
	
	?>

	<table border="0">
		<tr><td align="right">Username:</td><td align="left"><input type="text" size="20" name="regUsername" value="<?php if(!empty($_POST['regUsername'])) {echo $_POST['regUsername'];} ?>" /> (*)</td></tr>
		<tr><td align="right">Password:</td><td align="left"><input type="password" size="20" name="regPassword" value="<?php if(!empty($_POST['regPassword'])) {echo $_POST['regPassword'];} ?>" /> (*)</td></tr>
		<tr><td align="right">Re-type Password:</td><td align="left"><input type="password" size="20" name="regPassword2" value="<?php if(!empty($_POST['regPassword2'])) {echo $_POST['regPassword2'];} ?>" /> (*)</td></tr>
		<tr><td align="right">Email:</td><td align="left"><input type="text" size="20" name="regEmail" value="<?php if(!empty($_POST['regEmail'])) {echo $_POST['regEmail'];} ?>" /> (*)</td></tr>
		<tr><td align="right">First Name:</td><td align="left"><input type="text" size="20" name="regFirstName" value="<?php if(!empty($_POST['regFirstName'])) {echo $_POST['regFirstName'];} ?>" /> (*)</td></tr>
		<tr><td align="right">Last Name:</td><td align="left"><input type="text" size="20" name="regLastName" value="<?php if(!empty($_POST['regLastName'])) {echo $_POST['regLastName'];} ?>" /> (*)</td></tr>
		<tr><td colspan="2" align="center"><br /><input type="submit" value="Register" /></td></tr>
	</table>
	<input type="hidden" name="submitted" value="true" />
</form>

<?php
}
?>

</div>

<?php include("includes/footer.php"); ?>

</body>

</html>

<?php

// print out the buffer
ob_end_flush();

?>